﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AGrandVenture
{
    public class MeleeAttack
    {
        public enum AttackType { Swipe, Stab, Circle };

        public Sprite attackBase;

        private int AttackRange, AttackWidth;

        private Vector2 startLocation;

        private AttackType AtkType;

        private float arcProgress, swingSpeed = 0.0f;

        public MeleeAttack(int attackRange, int attackWidth, Vector2 attackLocation)
        {
            MeleeWeaponItem equippedWeapon = (MeleeWeaponItem) Player.Character.getItemInSlot(InventoryItem.EquipmentSlot.Primary);

            if (equippedWeapon != null)
            {
                Color effectColor = Color.Blue;
                this.startLocation = attackLocation;
                this.AttackRange = attackRange;
                this.AttackWidth = attackWidth;
                this.swingSpeed = (float)equippedWeapon.BaseAttackSpeed;


                //if (type = AttackType.swipe)
                //{
                this.attackBase = new Sprite(attackLocation,
                                             equippedWeapon.Base.Texture,
                                             equippedWeapon.Base.Source,
                                             Vector2.Zero,
                                             1.0f);

                attackBase.AddFrame(new Rectangle(0, 0, 48, 14), 0.5f);
                attackBase.TintColor = equippedWeapon.Base.TintColor;
                attackBase.Animate = false;
                attackBase.RelativeCentre = new Vector2(0, 48);
                
                //}
            }
        }

        public void Update(GameTime gameTime)
        {
            if (attackBase != null)
            {
                float xPos = 0.0f, yPos = 0.0f;
                Vector2 currentPosition;

                if (Player.MoveAngle.X >= 0)
                {
                    xPos += 48f;
                    yPos -= 24f;
                    attackBase.FlipFramesX(true);
                    attackBase.RelativeCentre = new Vector2(0, 40);
                }
                else
                {
                    xPos -= 48f;
                    yPos -= 24f;
                    attackBase.FlipFramesX(false);
                    attackBase.RelativeCentre = new Vector2(40, 40);
                }

                if (Player.MoveAngle.Y >= 0)
                {
                    //yPos -= 48f;
                    attackBase.FlipFramesY(true);
                }
                else
                {
                    xPos -= 48f;
                    yPos -= 24f;
                    attackBase.FlipFramesX(false);
                    attackBase.RelativeCentre = new Vector2(40, 40);
                }

                currentPosition = new Vector2(Player.WorldLocation.X + xPos, Player.WorldLocation.Y + yPos);
                attackBase.WorldLocation = currentPosition;
                arcProgress += 0.125f*swingSpeed;
                attackBase.Rotation = arcProgress;
                attackBase.Update(gameTime);
                if (arcProgress >= 2.5f)
                    attackBase = null;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (attackBase != null)
                attackBase.Draw(spriteBatch);   
        }
    }
}
